package com.ruoyi.server.api.coach.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.errorCode.CommonException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.validator.ValidatorUtil;
import com.ruoyi.server.api.coach.domain.CoachManagement;
import com.ruoyi.server.api.coach.mapper.CoachManagementMapper;
import com.ruoyi.server.api.coach.service.ICoachManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 教练信息Service业务层处理
 * 
 * @author ZLP
 * @date 2024-06-12
 */
@Service
public class CoachManagementServiceImpl implements ICoachManagementService
{
    @Autowired
    private CoachManagementMapper coachManagementMapper;

    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 查询教练信息
     * 
     * @param id 教练信息主键
     * @return 教练信息
     */
    @Override
    public CoachManagement selectCoachManagementById(Long id)
    {
        return coachManagementMapper.selectCoachManagementById(id);
    }

    /**
     * 查询教练信息列表
     * 
     * @param coachManagement 教练信息
     * @return 教练信息
     */
    @Override
    public List<CoachManagement> selectCoachManagementList(CoachManagement coachManagement)
    {
        String venueUniqueIdentifier = SecurityUtils.getLoginUser().getUser().getVenueUniqueIdentifier();
        coachManagement.setVenueUniqueIdentifier(venueUniqueIdentifier);
        return coachManagementMapper.selectCoachManagementList(coachManagement);
    }

    /**
     * 新增教练信息
     * 
     * @param coachManagement 教练信息
     * @return 结果
     */
    @Override
    public int insertCoachManagement(CoachManagement coachManagement) {
        // 判断对象是否为空
        try {
            if(ValidatorUtil.ValidationObject(coachManagement)){
                String venueUniqueIdentifier = SecurityUtils.getLoginUser().getUser().getVenueUniqueIdentifier();
                coachManagement.setVenueUniqueIdentifier(venueUniqueIdentifier);
                coachManagement.setCreateTime(DateUtils.getNowDate());
                String encode = bCryptPasswordEncoder.encode(coachManagement.getCoachPassword());
                coachManagement.setCoachPassword(encode);

                // 生成教练邀请码
                String s1 = IdUtils.randomUUID();
                String subString = s1.substring(9, 9 + 14);
                coachManagement.setCoachCode(subString);
            }
        } catch (CommonException e) {
            System.out.println("教练中的属性为空");
            throw new RuntimeException(e);
        }
        return coachManagementMapper.insertCoachManagement(coachManagement);
    }

    /**
     * 修改教练信息
     * 
     * @param coachManagement 教练信息
     * @return 结果
     */
    @Override
    public int updateCoachManagement(CoachManagement coachManagement)
    {
        try {
            if(ValidatorUtil.ValidationObject(coachManagement)){
                coachManagement.setUpdateTime(DateUtils.getNowDate());
                String venueUniqueIdentifier = SecurityUtils.getLoginUser().getUser().getVenueUniqueIdentifier();
                coachManagement.setVenueUniqueIdentifier(venueUniqueIdentifier);
            }
        } catch (CommonException e) {
            System.out.println("教练中的属性为空");
            throw new RuntimeException(e);
        }
        return coachManagementMapper.updateCoachManagement(coachManagement);
    }

    /**
     * 批量删除教练信息
     * 
     * @param ids 需要删除的教练信息主键
     * @return 结果
     */
    @Override
    public int deleteCoachManagementByIds(Long[] ids)
    {
        return coachManagementMapper.deleteCoachManagementByIds(ids);
    }

    /**
     * 删除教练信息信息
     * 
     * @param id 教练信息主键
     * @return 结果
     */
    @Override
    public int deleteCoachManagementById(Long id)
    {
        if(id != null && id != 0){
            return coachManagementMapper.deleteCoachManagementById(id);
        }else {
            return 2;
        }
    }
}
